package com.ruoyi.service.impl;

import java.util.List;
import java.util.jar.JarEntry;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.domain.AbilityUser;
import com.ruoyi.mapper.AbilityUserMapper;
import com.ruoyi.service.Consumer;
import com.ruoyi.service.IAbilityUserService;
import com.ruoyi.utils.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;


/**
 * 管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-20
 */
@Service
public class AbilityUserServiceImpl implements IAbilityUserService
{
    @Autowired
    private AbilityUserMapper abilityUserMapper;

    @Autowired
    Consumer consumer;

    /**
     * 查询管理
     * 
     * @param ID 管理主键
     * @return 管理
     */
    @Override
    public AbilityUser selectAbilityUserByID(Long ID)
    {
        return abilityUserMapper.selectAbilityUserByID(ID);
    }

    @Override
    public Integer blocking(AbilityUser abilityUser) {
        String cname=abilityUser.getUConsumer();
        String res="";
        boolean ishave=true;
        try {
            res=consumer.getit("consumers/"+cname+"/key-auth");
        }catch (HttpClientErrorException e){
            ishave=false;
        }
        if(ishave){
            String k="";
            try {
                 k=consumer.delAPIKey(res,cname);
            }catch (HttpClientErrorException e){
                return -1;
            }

          if(k.equals("ok")){
              AbilityUser newabilityUser=new AbilityUser();
              newabilityUser.setID(abilityUser.getID());
              newabilityUser.setStatus(0L);
              return abilityUserMapper.updateAbilityUser(newabilityUser);
          }
        }
        return null;
    }

    @Override
    public Integer unblocking(AbilityUser abilityUser) {
        String res="";
        try{
             res= consumer.APIKey(abilityUser.getUConsumer());
        }catch (Exception e){
            return 0;
        }

        JSONObject res1=JSONObject.parseObject(res);
        if(Check.CheckIsSuccess(res1)){
            AbilityUser newabilityUser=new AbilityUser();
            newabilityUser.setID(abilityUser.getID());
            newabilityUser.setStatus(1L);
            return abilityUserMapper.updateAbilityUser(newabilityUser);
        }
        return null;
    }

    /**
     * 查询管理列表
     * 
     * @param abilityUser 管理
     * @return 管理
     */
    @Override
    public List<AbilityUser> selectAbilityUserList(AbilityUser abilityUser)
    {
        return abilityUserMapper.selectAbilityUserList(abilityUser);
    }

    /**
     * 新增管理
     * 
     * @param abilityUser 管理
     * @return 结果
     */
    @Override
    public int insertAbilityUser(AbilityUser abilityUser)
    {
        return abilityUserMapper.insertAbilityUser(abilityUser);
    }

    /**
     * 修改管理
     * 
     * @param abilityUser 管理
     * @return 结果
     */
    @Override
    public int updateAbilityUser(AbilityUser abilityUser)
    {
        try {
            abilityUserMapper.updateAbilityUser(abilityUser);
        }catch (Exception e){
           if (e instanceof DataIntegrityViolationException){
               return -100;
           }
        }
        return -1;
    }

    /**
     * 批量删除管理
     * 
     * @param IDs 需要删除的管理主键
     * @return 结果
     */
    @Override
    public int deleteAbilityUserByIDs(Long[] IDs)
    {
        return abilityUserMapper.deleteAbilityUserByIDs(IDs);
    }

    /**
     * 删除管理信息
     * 
     * @param ID 管理主键
     * @return 结果
     */
    @Override
    public int deleteAbilityUserByID(Long ID)
    {
        return abilityUserMapper.deleteAbilityUserByID(ID);
    }
}
